﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Data;
using System.Data.OleDb;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using KoPortal.Sys.Data.Axapta;

namespace KoPortal.Sys.Metadata
{
    public static class MetadataSyncShops
    {
        #region Common Methods
        static public string StringConvertName(String mName)
        {
            return mName.Replace("\"", "'"); ;
        }
        #endregion Common Methods
        #region SystemSettings
        public static Exception GetLastsuccessfullyDateTime(ref DateTime retDT)
        {
            Exception ret = null;
            if (retDT == null)
                retDT = new DateTime();
            ret = KoEnvironment.GetSetting_CreateInDoc_LastSuccessfully(ref retDT);
            return ret;
        }
        #endregion
        public static Exception UpdateShops(DateTime dtFrom)
        {
            #region Members
            Exception ret = null;
            Exception err = null;
            int lcid = CultureInfo.CurrentCulture.LCID;
            string DomainNameStr = KoEnvironment.GetAppSetting("DomainName");
            int iAllRowsCount = 0;
            int iCurRow = 0;
            string errMsg = "";
            int ErrId = 0;
            int WarningLevel = 1;
            int RetCode = 0;            
            DateTime dtStart = DateTime.Now;
            DateTime dtEnd = DateTime.Now;
            DateTime dt = dtFrom;
            #endregion Members

            #region Pre Process
            DataTable tbl = null;
            //dt = new DateTime(dt.Year-1, dt.Month, dt.Day, 0, 0, 0);
            err = DBShops.GetDataByDataTime(ref tbl, dt);
            #endregion Pre Process

            using (SPSite site = new SPSite("http://localhost"))
            {
                iAllRowsCount += tbl.Rows.Count;
                iCurRow = 0;
                #region While
                foreach (DataRow row in tbl.Rows)
                {
                    // Job Progress :
                    iCurRow++;

                    if (row["ID"] == DBNull.Value)
                        continue;
                    if (!MetadataHelper.ExistItem(site, "Магазины", row["ID"].ToString(), lcid))
                    {
                        err = MetadataSyncBase.Insert(site, "Магазины", row["ID"].ToString()
                            , StringConvertName(row["NAME"].ToString())
                            , StringConvertName(row["Addr"].ToString())
                            , lcid);
                        if (err != null)
                        {
                            ret = err;
                            errMsg = err.Message;
                            ErrId = 2;
                            RetCode = 2;
                            KoEnvironment.Log_Add("Sync", "MetadataSyncShops", "Error", errMsg, "", ErrId, WarningLevel, RetCode, iAllRowsCount);
                            goto TheEndExecute;
                        }
                        // CreateLineLog("Add Shop : " + StringConvertName(reader["NAME"].ToString()));
                    }
                } // foreach
                #endregion While
            }

TheEndExecute:

            return ret;
        }
    }
}
